package com.promis.party.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.promis.party.R;
import com.promis.party.helper.ContextHelper;
import com.promis.party.helper.UIHepler;
import com.promis.party.ui.home.message.MessageActivity;
import com.promis.party.ui.learneducation.onlinelearning.exam.OnlinExam;
import com.promis.party.ui.learneducation.onlinelearning.requiredcourse.RequriedCourseActivity;
import com.promis.party.ui.partywork.dailywork.assessment.AssessmentActivity;
import com.promis.party.ui.partywork.organizeMag.partyDues.DuesActivity;
import com.promis.party.ui.userinfo.UserInfoActivity;
import com.promis.widget.FragmentTabHost;
import com.promis.widget.ImageTextView;
import com.promis.widget.listener.OnAvoidMultiClickListener;

/**
 * Created by zcy on 2018/9/25.
 */

public class HomeFragment extends Fragment {

    private ContextHelper contextHelper;

    private TabLayout tblyContainer;
    private ViewPager vpgvContainer;
    private FragmentPagerAdapter adapter;

    private ImageView imgvUserHead;
    private ImageView imgvLearnTime;
    private ImageTextView imgvMassage;
    private ImageTextView imgvDailyOffice;
    private ImageTextView imgvTrainHeadLine;
    private ImageTextView imgvRequiredCourse;
    private ImageTextView imgvPerformanceAppraisal;
    private ImageTextView imgvOnlineExam;
    private ImageTextView imgvIoneerRecommendation;
    private ImageTextView imgvPay;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FrameLayout rootLayout = (FrameLayout) inflater.inflate(R.layout.fragment_home, container, false);

        vpgvContainer = rootLayout.findViewById(R.id.fragment_home_vpgv_container);
        tblyContainer = rootLayout.findViewById(R.id.fragment_home_tbly_container);

        imgvUserHead = rootLayout.findViewById(R.id.fragment_home_rimgv_user_header);
        imgvLearnTime = rootLayout.findViewById(R.id.fragment_home_imgv_learntime);
        imgvMassage = rootLayout.findViewById(R.id.fragment_home_txtv_massage);
        imgvDailyOffice = rootLayout.findViewById(R.id.fragment_home_txtv_daily_office);
        imgvTrainHeadLine = rootLayout.findViewById(R.id.fragment_home_txtv_training_headlines);
        imgvRequiredCourse = rootLayout.findViewById(R.id.fragment_home_txtv_required_course);
        imgvPerformanceAppraisal = rootLayout.findViewById(R.id.fragment_home_txtv_performance_appraisal);
        imgvOnlineExam = rootLayout.findViewById(R.id.fragment_home_txtv_online_exam);
        imgvIoneerRecommendation = rootLayout.findViewById(R.id.fragment_home_txtv_ioneer_recommendation);
        imgvPay = rootLayout.findViewById(R.id.fragment_home_txtv_pay);

        initData();
        initListener();
        return rootLayout;
    }

    private void initData() {
        contextHelper = new ContextHelper(getContext());

        adapter = new FragmentPagerAdapter(getChildFragmentManager());
        vpgvContainer.setAdapter(adapter);
        tblyContainer.setupWithViewPager(vpgvContainer);
        for (int i = 0; i < tblyContainer.getTabCount() ; i++) {
            TabLayout.Tab tab = tblyContainer.getTabAt(i);
            tab.setCustomView(adapter.getTitleView());
        }
        UIHepler.setTabLayoutIndicator(tblyContainer, 0, 0);
    }

    private void initListener() {
        imgvUserHead.setOnClickListener(new OnAvoidMultiClickListener() {
            @Override
            public void onAvoidMultiClick(View v) {
                Intent intent = new Intent(getContext(), UserInfoActivity.class);
                startActivity(intent);
            }
        });

        imgvDailyOffice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTabHost tabHost = getActivity().findViewById(R.id.activity_main_tab_host);
                tabHost.setCurrentTab(1);
            }
        });

        imgvDailyOffice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTabHost tabHost = getActivity().findViewById(R.id.activity_main_tab_host);
                tabHost.setCurrentTab(1);
            }
        });

        imgvTrainHeadLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTabHost tabHost = getActivity().findViewById(R.id.activity_main_tab_host);
                tabHost.setCurrentTab(2);
            }
        });

        imgvRequiredCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), RequriedCourseActivity.class);
                startActivity(intent);
            }
        });

        imgvMassage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), MessageActivity.class);
                startActivity(intent);
            }
        });

        imgvPerformanceAppraisal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), AssessmentActivity.class);
                startActivity(intent);
            }
        });
        imgvOnlineExam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), OnlinExam.class);
                startActivity(intent);
            }
        });
        imgvPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DuesActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
//                case RequestCode.CITY : LetterSortActivity.Helper.onActivityResult(data, txtvCity); break;
//                case RequestCode.PETSELFISH :findOwnerPet(); break;
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

//    public void httpFindDynamicList() {
//        HttpJsonParams params = new HttpJsonParams();
//        params.add("interest",1);
//        if (UserPreferences.getInstance(getContext()).exists()) {
//            params.add("userId", UserPreferences.getInstance(getContext()).getId());
//        }
//        String url = contextHelper.getServiceUrl(R.string.findInterestDynamicList);
//        new HttpClient<JsonList<Dynamic>>(getContext()) {
//            @Override
//            protected void onSuccess(JsonList<Dynamic> result) {
//                dynamicList = result.getDataList();
//                adptAdopt.notifyDataSetChanged(dynamicList);
//            }
//        }.postAsyn(url, params);
//    }

    private class FragmentPagerAdapter extends FragmentStatePagerAdapter {

        public FragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0){
                return HomeFragmentBanner.newInstance(0);
            }else{
                return HomeFragmentBanner.newInstance(1);
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return String.valueOf(position + 1);
        }

        public View getTitleView() {
            TextView textView = new TextView(getContext());
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(25, 12);
            ViewGroup.MarginLayoutParams marginParams = null;
            if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
                marginParams = (ViewGroup.MarginLayoutParams) layoutParams;
            } else {
                marginParams = new ViewGroup.MarginLayoutParams(layoutParams);
            }
            marginParams.setMargins(10,0,10,0);
            textView.setLayoutParams(marginParams);
            textView.setBackgroundResource(R.drawable.fragment_home_tab_broder);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(10, 0, 10,0);
            return textView;
        }
    }
}